//
//  SCNInvestmentCell.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/3/15.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNInvestmentCell: UITableViewCell {

    //1
    lazy var bgView: UIView = {
       
        let view = UIView()
        view.backgroundColor = .white
        view.layer.borderWidth = 0.6
        view.layer.borderColor = SCNConstants.colorWithHexString("c7c7c7").cgColor
        return view
    }()
    //2
    lazy var topColorView: UIView = {
        
        let view = UIView()
        view.backgroundColor = SCNConstants.rootBackgroundColor
        return view
    }()
    //3
    lazy var titleL: UILabel = {
        
        let lab = UILabel()
        lab.textColor = SCNConstants.colorWithHexString("333333")
        lab.text = "守财奴新手专享标"
        lab.font = UIFont.systemFont(ofSize: 18.5)
    
        return lab
    }()
    //4
    lazy var tagL: UILabel = {
        
        let lab = UILabel()
        lab.backgroundColor = SCNConstants.colorWith255RGBA(100, g: 203, b: 164, a: 1)
        lab.text = "财"
        lab.textColor = .white
        lab.font = UIFont.systemFont(ofSize: 12.0)
        lab.textAlignment = .center
        lab.layer.cornerRadius = 9.0
        lab.layer.masksToBounds = true
        return lab
    }()
    //5
    lazy var huoImg: UIImageView = {
        
        let img = UIImageView(image: UIImage(named: "icon_huo"))
        
        return img
    }()
    //6
    lazy var repayL: UILabel = {
        
        let lab = UILabel()
        lab.backgroundColor = SCNConstants.colorWith255RGBA(244, g: 181, b: 44, a: 1)
        lab.text = "加息"
        lab.textColor = .white
        lab.font = UIFont.systemFont(ofSize: 13.0)
        lab.textAlignment = .center
        lab.layer.cornerRadius = 8.0
        lab.layer.masksToBounds = true
        return lab
    }()
    //7
    lazy var statusImg: UIImageView = {
        
        let img = UIImageView()
        
        return img
    }()
    //8
    lazy var retaLabel: UILabel = {
        
        let lab = UILabel()
        lab.textColor = SCNConstants.colorWithHexString("333333")
        lab.text = "年利率"
        lab.font = UIFont.systemFont(ofSize: 14.0)
        lab.textAlignment = .right
        return lab
    }()
    //9
    lazy var timeLabel: UILabel = {
        
        let lab = UILabel()
        lab.textColor = SCNConstants.colorWithHexString("333333")
        lab.text = "期限"
        lab.font = UIFont.systemFont(ofSize: 14.0)
        lab.textAlignment = .right
        return lab
    }()
    //10
    lazy var retaL: UILabel = {
        
        let lab = UILabel()
        lab.textColor = SCNConstants.colorWithHexString("ffa200")
        lab.text = "16.8%"
        lab.font = UIFont.boldSystemFont(ofSize: 23.0)
        
        return lab
    }()
    //11
    lazy var timeL: UILabel = {
        
        let lab = UILabel()
        lab.textColor = SCNConstants.rootBackgroundColor
        lab.text = "10天"
        lab.font = UIFont.boldSystemFont(ofSize: 23.0)
        
        return lab
    }()
    //12
    lazy var surplusMoneyL: UILabel = {

        let lab = UILabel()
        lab.textColor = SCNConstants.colorWithHexString("898989")
        lab.text = "剩余可投金额：100000元"
        lab.font = UIFont.systemFont(ofSize: 14.0)

        return lab
    }()

    //13
    lazy var progressView: SCNCircleLoadView = {
       
        let view = SCNCircleLoadView()
        return view
    }()

    //14
    lazy var hikeOutL: UILabel = {

        let lab = UILabel()
        lab.backgroundColor = SCNConstants.colorWithHexString("f45f48")
        lab.text = "+4.80%"
        lab.textColor = .white
        lab.font = UIFont.systemFont(ofSize: 12.0)
        lab.textAlignment = .center
        lab.layer.cornerRadius = 5.0
        lab.layer.masksToBounds = true
        return lab
    }()
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        contentView.backgroundColor = SCNConstants.tableViewBackgroundColor
     
        contentView.addSubview(bgView)
        bgView.addSubview(topColorView)
        bgView.addSubview(titleL)
        bgView.addSubview(tagL)
        bgView.addSubview(huoImg)
        bgView.addSubview(repayL)
        bgView.addSubview(statusImg)
        bgView.addSubview(retaL)
        bgView.addSubview(timeL)
        bgView.addSubview(surplusMoneyL)
        bgView.addSubview(progressView)
        bgView.addSubview(hikeOutL)
        
        makeConstraints()
    }
    
    
    func makeConstraints() {
        
        bgView.snp.makeConstraints { (make) in
            make.bottom.equalTo(contentView)
            make.leading.top.equalTo(10)
            make.trailing.equalTo(-10)
        }
        topColorView.snp.makeConstraints { (make) in
            make.leading.trailing.top.equalToSuperview()
            make.height.equalTo(10)
        }
        
        titleL.snp.makeConstraints { (make) in
            make.top.equalTo(25)
            make.leading.equalTo(12)
            make.size.equalTo(CGSize(width: 140, height: 20))
        }
        tagL.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 18, height: 18))
            make.leading.equalTo(titleL.snp.trailing).offset(5)
            make.top.equalTo(26)
        }
        huoImg.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 26, height: 26))
            make.leading.equalTo(tagL.snp.trailing).offset(5)
            make.top.equalTo(18)
        }
        repayL.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 40, height: 20))
            make.leading.equalTo(huoImg.snp.trailing).offset(5)
            make.top.equalTo(24)
        }
        statusImg.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 53, height: 53))
            make.trailing.equalTo(bgView)
            make.top.equalTo(10)
        }
        retaL.snp.makeConstraints { (make) in
            make.leading.equalTo(titleL)
            make.centerY.equalTo(contentView.snp.centerY).offset(18)
            make.size.equalTo(CGSize(width: 140, height: 40))
        }
        timeL.snp.makeConstraints { (make) in
            make.leading.equalTo(retaL.snp.trailing)
            make.centerY.equalTo(retaL)
            make.size.equalTo(CGSize(width: 110, height: 40))
        }
        surplusMoneyL.snp.makeConstraints { (make) in
            make.leading.equalTo(titleL)
            make.top.equalTo(retaL.snp.bottom).offset(5)
            make.size.equalTo(CGSize(width: 200, height: 20))
        }
        progressView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 65, height: 65))
            make.centerY.equalTo(retaL)
            make.trailing.equalTo(-20)
        }

        hikeOutL.snp.makeConstraints { (make) in
            
        }
    }
    
    public func setInvestmentTableViewCell(_ model: SCNInvestmentListModels, index: IndexPath, status: Int) {

        titleL.text = model.title
        
        let retaText = "\(model.originrate!)% 年利率"
        let timeText = "\(model.loandue!) 期限"
        retaL.attributedText = String().changeAttributedString(retaText, range: NSRange(location: retaText.characters.count - 3, length: 3), font: 14, color: SCNConstants.colorWithHexString("898989"))
        timeL.attributedText = String().changeAttributedString(timeText, range: NSRange(location: timeText.characters.count - 2, length: 2), font: 14, color: SCNConstants.colorWithHexString("898989"))
        
        if status == 1 {
            //投标中
            let surplusText = "剩余可投金额：\(model.balance!)元"
            surplusMoneyL.attributedText = String().changeAttributedString(surplusText, range: NSRange(location: 7, length: model.balance!.characters.count), font: 14.0, color: SCNConstants.colorWithHexString("ffa200"))
            
            progressView.progress = CGFloat(model.advance!)
        }else {
            //回款中
            surplusMoneyL.isHidden = true
            progressView.progress = 100
            progressView.circlePathLayer.strokeColor = SCNConstants.colorWithHexString("bbbbbb").cgColor
            progressView.progressTextL.text = "回款中"
            progressView.progressTextL.textColor = SCNConstants.colorWithHexString("bbbbbb")
        }
        
        
        
        if model.is_activity == "1" {
            tagL.text = "秒"
        }else if model.markTypeId == "3"{
            tagL.text = "财"
        }else if model.markTypeId == "4"{
            tagL.text = "新"
        }else if model.markTypeId == "7"{
            tagL.text = "赚"
        }else if model.markTypeId == "6"{
            tagL.text = "享"
        }else {
            tagL.text = "财"
        }
        
        if model.extrarate == "0.00" {
            hikeOutL.isHidden = true
            repayL.isHidden = true
            
        }else {
            hikeOutL.isHidden = false
            hikeOutL.text = "+\(model.extrarate!)%"
            repayL.isHidden = false
            
            let hikeOutLSzie = hikeOutL.text?.getTextRectSize(font: UIFont.systemFont(ofSize: 12.0),size: CGSize(width: 100, height: 20))


            hikeOutL.snp.makeConstraints({ (make) in
                make.trailing.equalTo(retaL).offset(-10)
                make.top.equalTo(retaL)
                make.size.equalTo(CGSize(width: (hikeOutLSzie?.width)! + 3, height: 15))
            })
            
        }
        

        var statusImageName = ""
        

        if model.isfirsttender == "1" {
            //新手
            huoImg.image = index.section == 0 ? UIImage(named: "icon_huo") : UIImage(named: "huohui_icon")
            huoImg.snp.updateConstraints({ (make) in
                make.size.equalTo(CGSize(width: 26, height: 26))
            })
            statusImageName = "scn_xinshou_biao_highlight_icon"
        }else {
            huoImg.snp.updateConstraints({ (make) in
                make.size.equalTo(CGSize(width: 0, height: 26))
            })
            statusImg.image = UIImage(named: "")
        }
        
        if model.is_activity == "1" {
            //秒杀
            statusImageName = "scn_xinshou_msbiaobiao_qbz_icon"
        }
        if model.show_rmb == 2 {
            //特约
            statusImageName = "scn_xinshou_biao_tyb_highlight_icon"
        }
        
        statusImg.image = UIImage(named: statusImageName)
        
        let titleSzie = titleL.text?.getTextRectSize(font: UIFont.systemFont(ofSize: 19.0),size: CGSize(width: 300, height: 20))

        titleL.snp.remakeConstraints { (make) in
            make.leading.equalTo(12)
            make.top.equalTo(25)
            make.size.equalTo(CGSize(width: (titleSzie?.width)!, height: 20))
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

